﻿namespace MicroCloud.Audit
{
    /// <summary>
    /// 服务实现：审计模块
    /// </summary>
    public class AuditService : IAuditContract
    {
        /// <summary>
        /// 初始化一个审计服务 <see cref="AuditService"/> 的新实例
        /// </summary>
        public AuditService(IServiceProvider provider)
        {
            ServiceProvider = provider;
            Logger = provider.GetLogger(GetType());
        }

        #region 属性
        /// <summary>
        /// 获取 服务提供者对象
        /// </summary>
        protected IServiceProvider ServiceProvider { get; }

        /// <summary>
        /// 获取 日志对象
        /// </summary>
        protected ILogger Logger { get; }

        /// <summary>
        /// 获取 事件总线
        /// </summary>
        protected IEventBus EventBus => ServiceProvider.GetService<IEventBus>();

        /// <summary>
        /// 实体审计存储
        /// </summary>
        protected AuditEntityStore AuditEntityStore=> ServiceProvider.GetService<AuditEntityStore>();

        /// <summary>
        /// 审计操作存储
        /// </summary>
        protected AuditOperationStore AuditOperationStore => ServiceProvider.GetService<AuditOperationStore>();

        /// <summary>
        /// 审计实体属性存储
        /// </summary>
        protected AuditPropertyStore AuditPropertyStore => ServiceProvider.GetService<AuditPropertyStore>();

        #endregion

        /// <summary>
        /// 获取 操作审计信息查询数据集
        /// </summary>
        public IQueryable<AuditOperation> AuditOperations => AuditOperationStore.AuditOperations.AsNoTracking();

        /// <summary>
        /// 获取 审计实体信息查询数据集
        /// </summary>
        public IQueryable<AuditEntity> AuditEntities => AuditEntityStore.AuditEntities.AsNoTracking();

        /// <summary>
        /// 获取 审计实体属性信息查询数据集
        /// </summary>
        public IQueryable<AuditProperty> AuditProperties => AuditPropertyStore.AuditProperties.AsNoTracking();

        /// <summary>
        /// 删除操作审计信息信息
        /// </summary>
        /// <param name="ids">要删除的操作审计信息编号</param>
        /// <returns>业务操作结果</returns>
        public virtual Task<OperationResult> DeleteAuditOperations(params long[] ids)
        {
            return AuditOperationStore.DeleteAsync(ids);
        }

        /// <summary>
        /// 删除数据审计信息信息
        /// </summary>
        /// <param name="ids">要删除的数据审计信息编号</param>
        /// <returns>业务操作结果</returns>
        public virtual Task<OperationResult> DeleteAuditEntities(params long[] ids)
        {
            return AuditEntityStore.DeleteAsync(ids);
        }

    }
}